<template>
  <div>
    <el-form :model="formData" label-width="100px" :inline="true">
      <el-form-item label="规则名称">
        <el-input v-model="formData.ruleName" clearable></el-input>
      </el-form-item>
      <el-form-item label="优惠模式">
        <el-select v-model="formData.discountsMode" clearable>
          <el-option :label="mode.label" :value="mode.value" v-for="mode in DiscountsMode" :key="mode.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="优惠类型">
        <el-select v-model="formData.discountCategory" clearable>
          <el-option :label="type.label" :value="type.value" v-for="type in DiscountCategory" :key="type.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规则状态">
        <el-select v-model="formData.ruleStatus" clearable>
          <el-option :label="status.label" :value="status.value" v-for="status in RuleStatus" :key="status.value"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="success" icon="el-icon-search" @click="doSearch">查询</el-button>
      <el-button type="primary" icon="el-icon-pulus" @click="addRule(null)">新增</el-button>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="ruleName" label="规则名称"></el-table-column>
      <el-table-column prop="mapDiscountsModeDesc" label="优惠模式" width="150"></el-table-column>
      <el-table-column prop="discountCategoryDesc" label="优惠类型" width="150"></el-table-column>
      <el-table-column prop="discountDesc" label="优惠额度" width="250"></el-table-column>
      <!-- <el-table-column prop="" label="创建日期" width="180">-</el-table-column> -->
      <el-table-column prop="activeDesc" label="状态" width="80"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <div>
            <!-- <el-button size="small" type="primary" v-if="!row.isActive" @click="addRule(row)">编辑</el-button> -->
            <el-button size="small" @click="triggerStatus(row)">{{ row.isActive ? '停用' : '启用' }}</el-button>
            <!-- <el-button size="small" type="danger" v-if="!row.isActive" @click="removeRule(row)">删除</el-button> -->
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { DiscountsMode, DiscountCategory, RuleStatus } from '@/constants';
export default {
  name: 'PartAmountRule',
  data() {
    return {
      RuleStatus,
      DiscountsMode,
      DiscountCategory,
      formData: {
        ruleName: '',
        discountsMode: undefined,
        discountCategory: undefined,
        ruleStatus: undefined,
      },
      tableData: [],
    };
  },
  beforeCreate() {
    this.pagination = {
      pageNum: 1,
      pageSize: 20,
      needTotalCount: true,
    };
  },
  mounted() {
    this.getMoneyRuleList();
  },
  methods: {
    addRule(data = undefined) {
      this.$emit('addRule', 'amount', data);
    },
    doSearch() {
      this.pagination.pageNum = 1;
      this.getMoneyRuleList();
    },
    mapdiscountCategory(category) {
      const ret = DiscountCategory.find((d) => d.value === category);
      return ret.label || '未知';
    },
    mapDiscountsMode(mode) {
      const ret = DiscountsMode.find((d) => d.value === mode);
      return ret.label || '未知';
    },
    getDiscountStr({ discountCategory, fullMoney, discountMoney }) {
      let ret = '';
      if (discountCategory === 1) {
        ret = `每满${fullMoney}元, 减${discountMoney}元`;
      } else if (discountCategory === 2) {
        ret = `立减${discountMoney}元`;
      } else if (discountCategory === 3) {
        ret = `打${+discountMoney * 10}折`;
      }
      return ret;
    },
    async triggerStatus({ id, isActive }) {
      const { success } = await this.$api.manageMoneyRule(
        id,
        isActive === 0 ? 1 : 2
      );

      success && this.getMoneyRuleList();
    },
    async removeRule({ id }) {
      this.$confirm('确定要删除此规则吗, 此操作无法恢复', '警告')
        .then(async() => {
          const { success } = await this.$api.manageMoneyRule(id, 3);
          success &&
            (this.getMoneyRuleList(), this.$message.success('删除成功'));
        })
        .catch(() => {});
    },
    async getMoneyRuleList() {
      const params = Object.assign({}, this.formData, {
        isActive: this.formData.ruleStatus
          ? this.formData.ruleStatus === 2
            ? 1
            : 0
          : '',
      });
      if (params.discountsMode === undefined) delete params.discountsMode;
      if (params.discountCategory === undefined) delete params.discountCategory;
      if (params.ruleStatus === undefined) delete params.ruleStatus;

      const { success, data } = await this.$api.getMoneyRuleList(params);
      if (success) {
        this.tableData = data.map((d) => ({
          ...d,
          discountCategoryDesc: this.mapdiscountCategory(d.discountCategory),
          mapDiscountsModeDesc: this.mapDiscountsMode(d.discountMode),
          activeDesc: d.isActive === 1 ? '启用' : '未启用',
          discountDesc: this.getDiscountStr(d),
        }));
      }
    },
    refresh() {
      this.getMoneyRuleList();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
